package com.yuanqi.dz.dc.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;
import com.yuanqi.dz.dc.adapters.ReadExcel_bak;
import com.yuanqi.dz.dc.util.DZConfigRead;
import com.yuanqi.dz.dc.util.FtpUtil;
import com.yuanqi.dz.dc.util.SftpClientUtil;
import com.yuanqitech.advancedpower.powerdate.DateUtil;
import com.yuanqitech.advancedpower.powerdate.PowerDateFormat;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

/**
 * 解析爱康医院  银联表格对账文件
 * @author HP
 *
 */
public class AiKangReadXlsServiceImpl  implements DZDataInterface{

	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");

	public static void main(String[] args) {
		DZInfoArray	dzInfoArray = new AiKangReadXlsServiceImpl().getDZData("898420280990113","20170301");
		DZInfo[] d = dzInfoArray.getDzInfo();
		if(null!=d && d.length>0){
			for (int i = 0; i < d.length; i++) {
				System.out.println(d[i]);
			}
			System.out.println(d.length);
		}
		
	}

	public  DZInfoArray getDZData(String mercharnum, String dzDate){
		DZInfoArray dzInfoArray = new DZInfoArray();
		Boolean download = false;
		String  is_private=DZConfigRead.getProperties("is_private");
		String source_type=null;
		String  address=null;
		String local_path =null;
		String fileName=null;
		String dz_date_format=null;
		String dz_date_addday=null;
		String ftp_port =null;
		String  username=null;
		String password = null;
		String  remotePath=null ;
		String is_filter = null;
		if(null!=is_private && "0".equals(is_private)){
			String  is_private_mercharnum=DZConfigRead.getProperties("is_private_"+mercharnum);
			if(null!=is_private_mercharnum && is_private_mercharnum.equals("1")){
				source_type=DZConfigRead.getProperties("source_type_"+mercharnum);
				address=DZConfigRead.getProperties("ftp_ip_"+mercharnum);
				local_path=DZConfigRead.getProperties("local_path_"+mercharnum);
				fileName=DZConfigRead.getProperties("fileName_"+mercharnum);
				dz_date_format=DZConfigRead.getProperties("dz_date_format_"+mercharnum);
				dz_date_addday=DZConfigRead.getProperties("dz_date_addday_"+mercharnum);
				ftp_port=DZConfigRead.getProperties("ftp_port_"+mercharnum);
				username=DZConfigRead.getProperties("ftp_username_"+mercharnum);
				password=DZConfigRead.getProperties("ftp_password_"+mercharnum);
				remotePath=DZConfigRead.getProperties("ftp_filepath_"+mercharnum);
				is_filter = DZConfigRead.getProperties("is_filter_"+mercharnum);
			}else{
				source_type=DZConfigRead.getProperties("source_type");
				address=DZConfigRead.getProperties("ftp_ip");
				local_path=DZConfigRead.getProperties("local_path");//本地目录
				fileName=DZConfigRead.getProperties("fileName");// 文件名称
				dz_date_format=DZConfigRead.getProperties("dz_date_format");//对账文件时间格式
				dz_date_addday=DZConfigRead.getProperties("dz_date_addday");  
				ftp_port=DZConfigRead.getProperties("ftp_port");
				username=DZConfigRead.getProperties("ftp_username");
				password=DZConfigRead.getProperties("ftp_password");
				remotePath=DZConfigRead.getProperties("ftp_filepath");
				is_filter = DZConfigRead.getProperties("is_filter");
			}

		}else{
			source_type=DZConfigRead.getProperties("source_type_"+mercharnum);
			address=DZConfigRead.getProperties("ftp_ip_"+mercharnum);
			local_path=DZConfigRead.getProperties("local_path_"+mercharnum);
			fileName=DZConfigRead.getProperties("fileName_"+mercharnum);
			dz_date_format=DZConfigRead.getProperties("dz_date_format_"+mercharnum);
			dz_date_addday=DZConfigRead.getProperties("dz_date_addday_"+mercharnum);
			ftp_port=DZConfigRead.getProperties("ftp_port_"+mercharnum);
			username=DZConfigRead.getProperties("ftp_username_"+mercharnum);
			password=DZConfigRead.getProperties("ftp_password_"+mercharnum);
			remotePath=DZConfigRead.getProperties("ftp_filepath_"+mercharnum);
			is_filter = DZConfigRead.getProperties("is_filter_"+mercharnum);

		}
		int port = 21;
		if(!StringHelper.isEmpty(ftp_port)){
			port = Integer.valueOf(ftp_port);
		}
		SimpleDateFormat sdf = new SimpleDateFormat(dz_date_format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(PowerDateFormat.getInstance(DateUtil.YYYYMMDD).parse(dzDate));
		fileName = fileName.replace("{merchant}", mercharnum).replace("{date}", sdf.format(calendar.getTime()));
		String path = local_path + fileName;
		File file = new File(local_path,fileName);
		try {
			if(null!=source_type &&  "1".equals(source_type)){
				if(file.exists()){
					dzInfoArray =  new ReadExcel_bak().getDataFromXls(path,mercharnum,is_filter);
				}else{
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp上未找到对账文件"+file.getName());
					LOG.addError(file.getAbsolutePath() +"未找到对账文件"+file.getName());
				}
			}else if(null!=source_type &&  "2".equals(source_type)){
				download = new FtpUtil(address,port,username,password,remotePath).downFile(file);
				try {
					download = new FtpUtil(address,port,username,password,remotePath).downFile(file);
				} catch (Exception e) {
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp异常");
					LOG.addError("连接ftp异常"+e);
					return dzInfoArray;
				}
				if (!download) {
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp上未找到对账文件"+file.getName());
					LOG.addError("ftp上未找到对账文件"+file.getName());
					return dzInfoArray;
				}
				dzInfoArray =  new ReadExcel_bak().getDataFromXls(path,mercharnum,is_filter);
			}else if(null!=source_type &&  "3".equals(source_type)){
				SftpClientUtil sftp =  new SftpClientUtil(address, port, username, password);
				sftp.connect();
				LOG.addInfo("从sftp下载对账文件,入参{remotePath:"+remotePath+",filename:"+fileName+",local_path:"+local_path+"}");
				download = sftp.download(remotePath, fileName, local_path);    
				if(!download){
					LOG.addError("下载对账文件失败"+file.getName());
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp上未找到对账文件"+file.getName());
				}
				dzInfoArray =  new ReadExcel_bak().getDataFromXls(path);
			}

		} catch (Exception e) {
			LOG.addError(e);
			dzInfoArray = new DZInfoArray();
			dzInfoArray.setRc("-1");
			dzInfoArray.setDzInfo(new DZInfo[0]);
			dzInfoArray.setErrormsg("获取渠道对账数据异常");
			return dzInfoArray;
		}

		return dzInfoArray;
	}
}
